home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / jetset.1 / jetset.tar / widget.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  7KB  |  264 lines

  1. #include <stdio.h>
  2.  
  3. #include "widget.h"
  4.  
  5.  
  6. static void ClassInitialize (void);
  7. static void Initialize (Widget, Widget, ArgList, Cardinal *);
  8. static void Realize (Widget, XtValueMask *, XSetWindowAttributes *);
  9. static void Destroy (Widget);
  10. static void Resize (Widget);
  11. static void HandleExpose (Widget, XEvent *, Region);
  12. static Boolean SetValues (Widget, Widget, Widget, ArgList, Cardinal *);
  13. static Boolean TakeFocus (Widget, Time *);
  14.  
  15.  
  16. extern void MoveLeft (MyWidget w, XEvent *event,
  17.               String *params, Cardinal *num_params);
  18. extern void MoveRight (MyWidget w, XEvent *event,
  19.                String *params, Cardinal *num_params);
  20. extern void Jump (MyWidget w, XEvent *event,
  21.           String *params, Cardinal *num_params);
  22. static void Enter (MyWidget w, XEvent *event,
  23.            String *params, Cardinal *num_params);
  24. static void Leave (MyWidget w, XEvent *event,
  25.            String *params, Cardinal *num_params);
  26.  
  27.  
  28. /*
  29.  * Definition of a new Widget.
  30.  */
  31.  
  32. static XtActionsRec actions[] = {
  33.     {"moveleft",    (XtActionProc) MoveLeft},
  34.     {"moveright",    (XtActionProc) MoveRight},
  35.     {"jump",        (XtActionProc) Jump},
  36.     {"enterwin",    (XtActionProc) Enter},
  37.     {"leavewin",    (XtActionProc) Leave},
  38. #if 0
  39.     {"putplayer",    (XtActionProc) PutPlayer},
  40. #endif
  41. };
  42.  
  43.  
  44. static char translations[] =
  45.     "<Key>comma:    moveleft()\n\
  46.      <KeyUp>comma:    moveleft()\n\
  47.      <Key>z:        moveleft()\n\
  48.      <KeyUp>z:        moveleft()\n\
  49.      <Key>o:        moveleft()\n\
  50.      <KeyUp>o:        moveleft()\n\
  51.      <Key>j:        moveleft()\n\
  52.      <KeyUp>j:        moveleft()\n\
  53.      <Key>Left:        moveleft()\n\
  54.      <KeyUp>Left:    moveleft()\n\
  55.      <Key>period:    moveright()\n\
  56.      <KeyUp>period:    moveright()\n\
  57.      <Key>x:        moveright()\n\
  58.      <KeyUp>x:        moveright()\n\
  59.      <Key>p:        moveright()\n\
  60.      <KeyUp>p:        moveright()\n\
  61.      <Key>l:        moveright()\n\
  62.      <KeyUp>l:        moveright()\n\
  63.      <Key>Right:    moveright()\n\
  64.      <KeyUp>Right:    moveright()\n\
  65.      <Key>space:    jump()\n\
  66.      <KeyUp>space:    jump()\n\
  67.      <Key>Up:        jump()\n\
  68.      <KeyUp>Up:        jump()\n\
  69.      <Key>k:        jump()\n\
  70.      <KeyUp>k:        jump()\n\
  71.      <EnterNotify>:    enterwin()\n\
  72.      <LeaveNotify>:    leavewin()";
  73.  
  74.  
  75.  
  76. MyClassRec myClassRec = {
  77.     {
  78. /* core_class fields */
  79.     /* superclass        */    (WidgetClass) &widgetClassRec,
  80.     /* class_name        */    "My",
  81.     /* widget_size        */    sizeof (MyRec),
  82.     /* class_initialize        */    ClassInitialize,
  83.     /* class_part_initialize    */    NULL,
  84.     /* class_inited        */    False,
  85.     /* initialize        */    Initialize,
  86.     /* initialize_hook        */    NULL,
  87.     /* realize            */    Realize,
  88.     /* actions            */    actions,
  89.     /* num_actions        */    XtNumber (actions),
  90.     /* resources        */    NULL, /* resources */
  91.     /* num_resources        */    0, /* XtNumber(resources) */
  92.     /* xrm_class        */    NULLQUARK,
  93.     /* compress_motion        */    True,
  94.     /* compress_exposure    */    False,
  95.     /* compress_enterleave    */    True,
  96.     /* visible_interest        */    False,
  97.     /* destroy            */    Destroy,
  98.     /* resize            */    Resize,
  99.     /* expose            */    HandleExpose,
  100.     /* set_values        */    SetValues,
  101.     /* set_values_hook        */    NULL,
  102.     /* set_values_almost    */    NULL,
  103.     /* get_values_hook        */    NULL,
  104.     /* accept_focus        */    TakeFocus,
  105.     /* version            */    XtVersion,
  106.     /* callback_private        */    NULL,
  107.     /* tm_table            */    translations,
  108.     /* query_geometry        */    NULL,
  109.     /* display_accelerator    */    NULL,
  110.     /* extension        */    NULL,
  111.   },{
  112. /* my_class field */
  113.     /* dummy            */    0             /* Make C compiler happy. */
  114.   }
  115. };
  116.  
  117. WidgetClass myWidgetClass = (WidgetClass) &myClassRec;
  118.  
  119.  
  120. /****************************************************/
  121.  
  122. /*
  123.  * Procedures
  124.  */
  125.  
  126.  
  127. static void ClassInitialize ()
  128. {
  129.     /* fprintf (stderr, "-- Class Initialize\n"); */
  130. }
  131.  
  132.  
  133. static void Initialize (Widget request, Widget w, ArgList al, Cardinal *cp)
  134. {
  135.     /* fprintf (stderr, "-- Initialize\n"); */
  136. }
  137.  
  138.  
  139. static void Realize (Widget ww, XtValueMask *valueMask,
  140.              XSetWindowAttributes *attributes)
  141. {
  142.     MyWidget w = (MyWidget) ww;
  143.     XWindowAttributes watr;
  144.     XGCValues gcv;
  145.     GC gc;
  146.     Display *disp;
  147.     Window win;
  148.     Pixmap store;
  149.  
  150.     /* fprintf (stderr, "-- Realize\n"); */
  151.  
  152.     if (w->core.width == 0)
  153.     w->core.width = 1;
  154.     if (w->core.height == 0)
  155.     w->core.height = 1;
  156.     disp = XtDisplay (w);
  157.     XtCreateWindow ((Widget) w, InputOutput,
  158.             DefaultVisual (disp, DefaultScreen (disp)),
  159.             *valueMask, attributes);
  160.     win = XtWindow (w);
  161.     XGetWindowAttributes (disp, win, &watr);
  162.     store = XCreatePixmap (disp, win, watr.width, watr.height, watr.depth);
  163.     w->my.store = store;
  164.     gcv.foreground = WhitePixel (disp, DefaultScreen (disp));
  165.     gc = XCreateGC (disp, store, GCForeground, &gcv);
  166.     w->my.gc = gc;
  167.     XFillRectangle (disp, store, gc, 0, 0, watr.width, watr.height);
  168. }
  169.  
  170.  
  171. static void HandleExpose (Widget ww, XEvent *event, Region r)
  172. {
  173.     MyWidget w = (MyWidget) ww;
  174.     /* fprintf (stderr, "-- Handle Expose\n"); */
  175.  
  176.     XCopyArea (XtDisplay (w), w->my.store, w->core.window,
  177.            w->my.gc,
  178.            event->xexpose.x, event->xexpose.y,
  179.            event->xexpose.width, event->xexpose.height,
  180.            event->xexpose.x, event->xexpose.y);
  181. }
  182.  
  183.  
  184. extern void set_colormaps (int on);
  185. extern MyWidget canvas_widget;
  186.  
  187. static void Leave (w, event, params, num_params)
  188.     MyWidget w;
  189.     XEvent *event;
  190.     String *params;
  191.     Cardinal *num_params;
  192. {
  193.     if (w == canvas_widget)
  194.     set_colormaps (0);
  195.     XAutoRepeatOn (XtDisplay (w));
  196. }
  197.  
  198. static void Enter (w, event, params, num_params)
  199.     MyWidget w;
  200.     XEvent *event;
  201.     String *params;
  202.     Cardinal *num_params;
  203. {
  204.     extern int goleft, goright, dojump;
  205.  
  206.     if (w == canvas_widget)
  207.     set_colormaps (1);
  208.     XAutoRepeatOff (XtDisplay (w));
  209.     goleft = 0;
  210.     goright = 0;
  211.     dojump = 0;
  212. }
  213.  
  214.  
  215.  
  216. static void Destroy (Widget w)
  217. {
  218.     /* fprintf (stderr, "-- Destroy\n"); */
  219. }
  220.  
  221. static void Resize (Widget w)
  222. {
  223.     /* fprintf (stderr, "-- Resize\n"); */
  224. }
  225.  
  226.  
  227. static Boolean SetValues (Widget w1, Widget w2, Widget w3, ArgList al, Cardinal *cp)
  228. {
  229.     /* fprintf (stderr, "-- Set Values\n"); */
  230. }
  231.  
  232. static Boolean TakeFocus(Widget w, Time *time)
  233. {
  234.     /* fprintf (stderr, "-- Take Focus\n"); */
  235.  
  236.     XSetInputFocus (XtDisplay (w), w->core.window, RevertToPointerRoot,
  237.             CurrentTime);
  238.     return True;
  239. }
  240.  
  241.  
  242.  
  243.  
  244. /*********** User procedures ***********/
  245.  
  246.  
  247.  
  248.  
  249. void Update_Screen (MyWidget w)
  250. {
  251.     XCopyArea (XtDisplay (w), w->my.store, w->core.window,
  252.            w->my.gc,
  253.            0, 0, w->core.width, w->core.height, 0, 0);
  254. }
  255.  
  256. void Update_Zone (MyWidget w, int x, int y, int width, int height)
  257. {
  258.     XCopyArea (XtDisplay (w), w->my.store, w->core.window,
  259.            w->my.gc,
  260.            x, y, width, height, x, y);
  261. }
  262.  
  263.  
  264.